package org.zero.concurrency.demo.example.singleton;

import org.zero.concurrency.demo.annotations.NoRecommend;
import org.zero.concurrency.demo.annotations.ThreadSafe;

/**
 * 同步懒汉式单例-线程不安全-实例在第一次使用的时候创建
 *
 * @author 01
 */
@ThreadSafe
@NoRecommend
public class SingletonExample3 {

    /**
     * 单例对象
     */
    private static SingletonExample3 instance = null;

    /**
     * 私有构造函数
     */
    private SingletonExample3() {
    }

    /**
     * 工厂方法-获取实例
     * 加上了synchronized后，多线程访问性能很差
     *
     * @return instance
     */
    public synchronized static SingletonExample3 getInstance() {
        if (instance == null) {
            instance = new SingletonExample3();
        }
        return instance;
    }
}
